# core/autoloads/_attribute_system.gd (Autoload单例)
extends Node

# === 基础属性定义 ===
var base_stamina: float = 100.0              # 基础体能
var base_hunger: float = 100.0               # 基础饥饿值
var base_thirst: float = 100.0               # 基础口渴值
var base_heart_rate: float = 80.0            # 基础心率（正常范围80-180）
var base_fatigue: float = 0.0                # 基础疲劳度（0-100%）

var base_recovery_rate: float = 3.0          # 基础恢复速率

# === 实时属性值 ===
var stamina: float:
	set(value):
		base_stamina = clamp(value, 0, 200.0)
		_EventBus.stamina_updated.emit(base_stamina)

var hunger: float:
	set(value):
		base_hunger = clamp(value, 0, 100.0)
		_EventBus.hunger_updated.emit(base_hunger)

var thirst: float:
	set(value):
		base_thirst = clamp(value, 0, 100.0)
		_EventBus.thirst_updated.emit(base_thirst)

var heart_rate: float:
	set(value):
		base_heart_rate = clamp(value, 60.0, 200.0)
		_EventBus.heart_rate_updated.emit(base_heart_rate)

var fatigue: float:
	set(value):
		base_fatigue = clamp(value, 0.0, 100.0)
		_EventBus.fatigue_updated.emit(base_fatigue)
# === 属性修饰器系统 ===
var _modifiers := {}  # Key: 属性名, Value: { "multiplier": float, "timer": float }

# ----------------------------
# 公开方法
# ----------------------------
func _ready() -> void:
	stamina = 150.0
	hunger = 80.0
	thirst = 60.0
	
func apply_temporary_boost(attribute: String, multiplier: float, duration: float) -> void:
	"""应用临时属性增益/减益"""
	_modifiers[attribute] = {
		"multiplier": multiplier,
		"timer": duration
	}
	_update_attribute(attribute)

func reset_attributes() -> void:
	"""重置所有属性到初始状态"""
	stamina = base_stamina
	hunger = base_hunger
	thirst = base_thirst
	_modifiers.clear()

# ----------------------------
# 内部逻辑
# ----------------------------
func _update_attribute(attribute: String) -> void:
	"""更新属性值并触发事件"""
	match attribute:
		"stamina":
			var final = base_stamina * _calculate_multiplier(attribute)
			stamina = final
		"hunger":
			var final = base_hunger * _calculate_multiplier(attribute)
			hunger = final
		"thirst":
			var final = base_thirst * _calculate_multiplier(attribute)
			thirst = final

func _calculate_multiplier(attribute: String) -> float:
	"""计算属性综合修正系数"""
	var multiplier = 1.0
	if _modifiers.has(attribute):
		multiplier *= _modifiers[attribute].multiplier
	return multiplier

func _process(delta: float) -> void:
	"""逐帧更新修饰器计时器"""
	for attribute in _modifiers.keys():
		_modifiers[attribute].timer -= delta
		if _modifiers[attribute].timer <= 0:
			_modifiers.erase(attribute)
			_update_attribute(attribute)
